/*
 * HashBucket_test.cpp
 *
 *  Created on: Oct 28, 2012
 *      Author: m
 */

#include <memory>
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <sstream>

#include "HashBucket.h"

#define _HB_CAPACITY 4+4+32*4
#define _HB_ID 0
#define _HB_TD 1
// nota: 32 bytes por registro
#define _REG1_ID 1
#define _REG1_PHRASE std::string("frase_1")
#define _REG1_AUTHOR_SURNAME std::string("apellido_1")
#define  _REG1_AUTHOR_NAME std::string("nombre_1")
#define _REG2_ID 2
#define _REG2_PHRASE std::string("frase_2")
#define _REG2_AUTHOR_SURNAME std::string("apellido_2")
#define _REG2_AUTHOR_NAME std::string("nombre_2")
#define _REG3_ID 3
#define _REG3_PHRASE std::string("frase_3")
#define _REG3_AUTHOR_SURNAME std::string("apellido_3")
#define _REG3_AUTHOR_NAME std::string("nombre_3")
#define _REG4_ID 4
#define _REG4_PHRASE std::string("frase_4")
#define _REG4_AUTHOR_SURNAME std::string("apellido_4")
#define _REG4_AUTHOR_NAME std::string("nombre_4")
#define _REG5_ID 5
#define _REG5_PHRASE std::string("frase_5")
#define _REG5_AUTHOR_SURNAME std::string("apellido_5")
#define _REG5_AUTHOR_NAME std::string("nombre_5")

int main( int argc, char* argv[] )
{
    HashRecord *pr;

    // Creo un Bucket nuevo
    HashBucket hb( _HB_CAPACITY, _HB_ID, _HB_TD );

    // Varifico que se haya inicializado correctamente
    assert( hb.getBucketNr() == _HB_ID );
    assert( hb.getTd() == _HB_TD );
    assert( hb.getContent().empty() );

    // Varifico correcto funcionamiento del set/get de tamaño de dispersion
    hb.setTd( 2 );
    assert( hb.getTd() == 2 );

    // Añado satisfactoriamente 4 registros
    assert( hb.addRegister( _REG1_ID, _REG1_PHRASE, _REG1_AUTHOR_SURNAME, _REG1_AUTHOR_NAME ) );
    assert( hb.addRegister( _REG2_ID, _REG2_PHRASE, _REG2_AUTHOR_SURNAME, _REG2_AUTHOR_NAME ) );
    assert( hb.addRegister( _REG3_ID, _REG3_PHRASE, _REG3_AUTHOR_SURNAME, _REG3_AUTHOR_NAME ) );
    assert( hb.addRegister( _REG4_ID, _REG4_PHRASE, _REG4_AUTHOR_SURNAME, _REG4_AUTHOR_NAME ) );

    // Añado un 5to registro que no va a entrar
    assert( hb.addRegister( _REG5_ID, _REG5_PHRASE, _REG5_AUTHOR_SURNAME, _REG5_AUTHOR_NAME ) == false );

    // Verifico contenido del Bucket
    pr = hb.getRecord( _REG1_ID );
    assert(pr->getId() == _REG1_ID );
    assert(pr->getFrase().compare( _REG1_PHRASE ) == 0 );
    assert(pr->getAutorApellido().compare( _REG1_AUTHOR_SURNAME ) == 0 );
    assert(pr->getAutorNombre().compare( _REG1_AUTHOR_NAME ) == 0 );

    pr = hb.getRecord( _REG2_ID );
    assert(pr->getId() == _REG2_ID );
    assert(pr->getFrase().compare( _REG2_PHRASE ) == 0 );
    assert(pr->getAutorApellido().compare( _REG2_AUTHOR_SURNAME ) == 0 );
    assert(pr->getAutorNombre().compare( _REG2_AUTHOR_NAME ) == 0 );

    pr = hb.getRecord( _REG3_ID );
    assert(pr->getId() == _REG3_ID );
    assert(pr->getFrase().compare( _REG3_PHRASE ) == 0 );
    assert(pr->getAutorApellido().compare( _REG3_AUTHOR_SURNAME ) == 0 );
    assert(pr->getAutorNombre().compare( _REG3_AUTHOR_NAME ) == 0 );

    pr = hb.getRecord( _REG4_ID );
    assert(pr->getId() == _REG4_ID );
    assert(pr->getFrase().compare( _REG4_PHRASE ) == 0 );
    assert(pr->getAutorApellido().compare( _REG4_AUTHOR_SURNAME ) == 0 );
    assert(pr->getAutorNombre().compare( _REG4_AUTHOR_NAME ) == 0 );

    assert( hb.getContent().size() == 4 );

    // Borro el registro 1
    assert( hb.recordExists( _REG1_ID ) );
    hb.deleteRecord( _REG1_ID );
    assert( hb.recordExists( _REG1_ID ) == false );
    assert( hb.getContent().size() == 3 );

    // Re-inserto el registro 5
    assert( hb.recordExists( _REG5_ID ) == false );
    assert( hb.addRegister( _REG5_ID, _REG5_PHRASE, _REG5_AUTHOR_SURNAME, _REG5_AUTHOR_NAME ) );
    assert( hb.recordExists( _REG5_ID ) );
    assert( hb.getContent().size() == 4 );

    // Borro el contenido del Bucket
    hb.eraseContent();
    assert( hb.getContent().size() == 0 );

    return 0;
}




